21 Lecture
CS506
Midterm & Final Term Short Notes
Socket Programming
Socket programming involves creating network connections between computers using sockets. It enables data exchange for applications over a network, allowing communication and data sharing through defined protocols. Sockets facilitate seamless in
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
Sure, here are 10 multiple-choice questions (MCQs) related to Socket Programming along with their solutions and multiple options:
**Question 1: What is a socket in the context of networking and programming?**
A) A type of cable used to connect computers.
B) A software component for sending and receiving data over a network.
C) A type of protocol used for secure data transfer.
D) A hardware device used to route network traffic.
**Solution: B**
**Question 2: Which protocol is commonly used for socket programming on the internet?**
A) HTTP
B) FTP
C) SMTP
D) TCP/IP
**Solution: D**
**Question 3: Which function is used to create a socket in most socket programming APIs?**
A) `open_socket()`
B) `create_socket()`
C) `socket()`
D) `new_socket()`
**Solution: C**
**Question 4: What is the role of the server in socket programming?**
A) Sending requests to clients.
B) Processing client requests and providing responses.
C) Providing internet access to clients.
D) Managing network infrastructure.
**Solution: B**
**Question 5: In socket programming, what does the term "IP address" refer to?**
A) Internet Provider address of the server.
B) Internal Processor address of the computer.
C) Internet Protocol address of a network interface.
D) Internal Port address of the application.
**Solution: C**
**Question 6: Which socket type is used for connection-oriented communication in TCP?**
A) SOCK_STREAM
B) SOCK_DGRAM
C) SOCK_RAW
D) SOCK_SEQPACKET
**Solution: A**
**Question 7: What does the term "port number" signify in socket programming?**
A) It's a unique identifier for a computer on the internet.
B) It's a code that represents the physical location of a server.
C) It's an integer that identifies a specific process on a host.
D) It's a measure of the bandwidth of a network connection.
**Solution: C**
**Question 8: Which socket API function is used to establish a connection between a client and a server?**
A) connect()
B) send()
C) bind()
D) listen()
**Solution: A**
**Question 9: What is the purpose of the `bind()` function in socket programming?**
A) To associate a socket with a local IP address and port number.
B) To establish a connection between client and server.
C) To send data over the network.
D) To close a socket gracefully.
**Solution: A**
**Question 10: Which socket type is used for connectionless communication in UDP?**
A) SOCK_STREAM
B) SOCK_DGRAM
C) SOCK_RAW
D) SOCK_SEQPACKET
**Solution: B**
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
Certainly, here are 10 short subjective questions related to Socket Programming along with their answers:
**Question 1: What is Socket Programming?**
**Answer:** Socket programming is a technique in computer networking that enables communication between processes running on different devices over a network using sockets, which are endpoints for sending and receiving data.
**Question 2: Explain the client-server model in Socket Programming.**
**Answer:** The client-server model involves two types of processes: the client, which requests services, and the server, which provides services. In socket programming, the client establishes a connection to the server's socket, allowing them to exchange data.
**Question 3: What is a socket?**
**Answer:** A socket is an endpoint for sending or receiving data across a computer network. It is identified by an IP address and a port number and is used to establish a connection between two computers.
**Question 4: What is the difference between TCP and UDP sockets?**
**Answer:** TCP (Transmission Control Protocol) sockets provide reliable, connection-oriented communication with error checking and data sequencing. UDP (User Datagram Protocol) sockets provide connectionless, unreliable communication without error checking and sequencing.
**Question 5: How is a socket identified in a network connection?**
**Answer:** A socket is identified by a combination of an IP address and a port number. The IP address locates the device, while the port number identifies a specific application or process on that device.
**Question 6: What is the purpose of the `bind()` function in socket programming?**
**Answer:** The `bind()` function is used to associate a socket with a specific IP address and port number on the local machine. This allows the socket to listen for incoming connections or to send data from that address.
**Question 7: Explain the steps involved in establishing a TCP connection using socket programming.**
**Answer:** The steps are:
1. Server creates a socket using `socket()` and binds it using `bind()`.
2. Server listens for incoming connections using `listen()`.
3. Client creates a socket using `socket()`.
4. Client initiates a connection to the server using `connect()`.
5. Server accepts the incoming connection using `accept()`.
**Question 8: What is the role of the `accept()` function in socket programming?**
**Answer:** The `accept()` function is used by a server to accept an incoming connection request from a client. It creates a new socket for communication with that client and returns the socket's descriptor.
**Question 9: How does error handling work in socket programming?**
**Answer:** Error handling involves checking return values of socket functions. Negative values indicate errors, and you can use functions like `perror()` to print error messages. Common errors include connection failures, timeouts, and invalid addresses.
**Question 10: Can a single program act as both a client and a server using socket programming?**
**Answer:** Yes, a program can act as both a client and a server by implementing both client-side and server-side socket functionalities. This is often seen in peer-to-peer applications where devices communicate directly with each other.